package com.xxx.service.impl;

import com.xxx.mapper.ClassInfoMapper;
import com.xxx.mapper.PackageMapper;
import com.xxx.pojo.Package;
import com.xxx.service.Services;
import com.xxx.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


import java.util.List;

public class PackageServiceImpl implements Services<Package> {
    SqlSessionFactory factory = SqlSessionFactoryUtil.getSqlSessionFactory();

    /**
     * 查询所有套餐名称
     * @param username
     * @return
     */
    @Override
    public Package selectByUsername(String username) {
        SqlSession sqlSession = factory.openSession();
        PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
        Package aPackage = mapper.selectByName(username);
        sqlSession.close();
        return aPackage;
    }

    @Override
    public List<Package> selectAll(Integer index, Integer num) {
        SqlSession sqlSession = factory.openSession();
        PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
        List<Package> list = mapper.selectAll(index,num);
        sqlSession.close();
        return list;
    }

    @Override
    public int add(Package p) {
        SqlSession sqlSession = factory.openSession();
        PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
        int i = mapper.addPackage(p);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }



    @Override
    public int delete(Long id) {
        SqlSession sqlSession = factory.openSession();
        PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
        int i = mapper.delByCode(id);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

    @Override
    public Package selectById(Long id) {
        return null;
    }

    @Override
    public int updateById(Package p) {
        SqlSession sqlSession = factory.openSession();
        PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
        int i = mapper.updatePackage(p);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }



    @Override
    public int selectCount() {
        SqlSession sqlSession = factory.openSession();
        PackageMapper mapper = sqlSession.getMapper(PackageMapper.class);
        int count = mapper.getCount();
        sqlSession.close();

        return count;
    }

    @Override
    public List<String> selectAllByTypesName() {
        SqlSession sqlSession = factory.openSession();
        ClassInfoMapper mapper = sqlSession.getMapper(ClassInfoMapper.class);
        List<String> list = mapper.selectAllByTypesName();
        list.removeIf((String s)->{
            if (!s.contains("套餐")){
                return true;
            }
            return false;
        });
        sqlSession.close();
        return list;
    }
}
